<?php
abstract class At_Model_Mapper{
    protected $dbTable;
    protected $tableName = 'keyword';

    public function fetchAll( $model ){
        $results = $this->getTable()->fetchAll();
        $collect = array();

        $className = get_class( $model );

        foreach( $results as $row ){
            $collect[] = new $className( $row->toArray() );
        }

        return $collect;
    }

    public function find( $model ){
        $result = $this->getTable()->find( $model->id );

        if( count( $result->current() ) > 0 ){
            foreach( $result->current()->toArray() as $key => $value ){
                $model->$key = $value;
            }
        }
    }

    public function delete( $model ){
        $this->getTable()->delete(array('id = ?' => $model->id));
    }

    protected function getTable(){
        if( $this->dbTable === null ){
			$class = sprintf( 'Default_Model_DbTable_%s',
				join( '', array_map( 'ucfirst', explode( '_', $this->tableName ) ) ) );
            $dbTable = new $class();
            $this->setTable( $dbTable );
        }
        return $this->dbTable;
    }

    protected function setTable( $dbTable ){
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->dbTable = $dbTable;
    }
}
